package com.example.springbootswaggersimple.config;

import com.google.common.base.Predicates;
import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.MappedInterceptor;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.URL;
import java.nio.file.Paths;

/**
 * @author linjy on 2022/1/25
 */
@Configuration
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
@EnableSwagger2//旧版本 登录方式(http://localhost:9988/swagger-ui.html)
public class SwaggerConfig {

    @Value("${swagger.basic.username:admin}")
    private String username;
    @Value("${swagger.basic.password:admin}")
    private String password;

    /**
     * 用于账号密码登录
     * 必须在此处配置拦截器,要不然拦不到swagger的静态资源
     */
    @Bean
    @ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
    public MappedInterceptor getMappedInterceptor() {
        return new MappedInterceptor(new String[]{"/swagger-ui.html", "/webjars/**"},
                new SwaggerInterceptor(username, password));
    }

    @Bean
    public Docket buildDocket() {
        /*return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();*/
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.*"))
                .paths(Predicates.or(PathSelectors.ant("/*/**"),
                        PathSelectors.ant("/saveBiopsyInfo/**")))
                .build();
    }

    private ApiInfo buildApiInf() {
        // 作者信息
        Contact contact = new Contact("linjy", "", "923172094@qq.com");
        return new ApiInfoBuilder()
                .title("NightOwl RESTful APIs")
                .description("关注我 http://hwangfantasy.github.io/")
                .termsOfServiceUrl("http://hwangfantasy.github.io/")
                .contact(contact)
                .version("1.0")
                .build();
    }

    /**
     * 可以生成YAML格式的文档
     * https://editor.swagger.io
     */

    /**
     * 生成AsciiDocs格式文档
     *
     * @throws Exception
     */
    @Test
    public void generateAsciiDocs() throws Exception {
        //    输出Ascii格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();
        // swagger-ui.html页面中能找到此链接
        Swagger2MarkupConverter.from(new URL("http://localhost:9988/v2/api-docs"))
                .withConfig(config)
                .build()
                //.toFolder(Paths.get("./docs/asciidoc/generated"));
                .toFile(Paths.get("./docs/asciidoc/generated/all"));
    }

    /*@Test
    public void generateMarkdownDocs() throws Exception {
        //    输出Ascii格式
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.CONFLUENCE_MARKUP)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();
        // swagger-ui.html页面中能找到此链接
        Swagger2MarkupConverter.from(new URL("http://localhost:8090/v2/api-docs"))
                .withConfig(config)
                .build()
                //.toFolder(Paths.get("./docs/markdown/generated"));
                .toFile(Paths.get("./docs/confluence/generated/all"));
    }*/

}
